package cn.zymall.order.service.impl;

import cn.zymall.common.model.HomeBillboards;
import cn.zymall.common.model.Order;
import cn.zymall.common.model.Sku;
import cn.zymall.common.utils.redis.RedisUtil;
import cn.zymall.order.dao.OrderMapper;
import cn.zymall.order.dao.SkuMapper;
import cn.zymall.order.service.AliPayService;
import cn.zymall.order.service.OrderService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: mall
 * @description: 订单业务实现类
 * @author: YueNiao
 * @email: 1486617253@qq.com
 * @create: 2020-08-17 22:07
 **/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
	@Autowired
	private RedissonClient redissonClient;
	@Autowired
	private SkuMapper skuMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	AliPayService aliPayService;
	@Autowired
	RedisUtil redisUtil;

	/**
	 * 下单调用支付
	 */

	@Override
	public boolean orderPay(HttpServletRequest request, HttpServletResponse response, Order order) throws IOException {
		Order newOrder = decrementProductStore(order);
		if(newOrder==null){
				return  false;
		}
			aliPayService.toPay(request,response,newOrder);
			return true;
		}

	/**
	 * 动态查询订单
	 * @param userId
	 * @param orderStatus
	 * @return
	 */
	@Override
	public PageInfo<Order> queryOrder(String userId, String orderStatus,int page ,int rows) {
		PageHelper.startPage(page,rows);
		List<Order> list=orderMapper.queryOrder(userId,orderStatus);
		PageInfo<Order> pageaa = new PageInfo<>(list);
		return pageaa;
	}

	/**
	 * 模糊查询
	 * @param userId
	 * @param orderStatus
	 * @param value
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public PageInfo<Order> dimQueryOrder(String userId,String orderStatus,String value, int page ,int rows) {
		PageHelper.startPage(page,rows);
		List<Order> list=orderMapper.dimQueryOrder(userId,orderStatus,value);
		PageInfo<Order> pageaa = new PageInfo<>(list);
		return pageaa;
	}

	/**
	 * 商家订单查询
	 * @param merchantId
	 * @param orderStatus
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public PageInfo<Order> merchantQueryOrder(String merchantId, String orderStatus, int page, int rows) {
		PageHelper.startPage(page,rows);
		List<Order> list=orderMapper.merchantQueryOrder(merchantId,orderStatus);
		PageInfo<Order> pageaa = new PageInfo<>(list);
		return pageaa;
	}

	/**
	 * 店家模糊查询
	 * @param merchantId
	 * @param orderStatus
	 * @param value
	 * @param page
	 * @param rows
	 * @return
	 */
	@Override
	public PageInfo<Order> dimMerchantQueryOrder(String merchantId, String orderStatus, String value, int page, int rows) {
		PageHelper.startPage(page,rows);
		List<Order> list=orderMapper.dimMerchantQueryOrder(merchantId,orderStatus,value);
		PageInfo<Order> pageaa = new PageInfo<>(list);
		return pageaa;
	}

	;
		/**
		 * 创建订单
		 * @param order
		 * @return
		 */
		@Override
		public Order decrementProductStore(Order order) {
			/**
			 * 存入失败返回
			 */
			if(!redisDecrementOroduct(order)){
				return null;
			}

			String key = "dec_store_lock_" + order.getSpuId();
			RLock lock = redissonClient.getLock(key);
			Order createOrder=null;
			try {
				//加锁
				lock.lock();
				Sku productInfo=skuMapper.selectById(order.getSkuId());
				//如果库存为空
				if (productInfo.getSum() == 0) {
					return null;
				}
				//减库存操作
				productInfo.setSum(productInfo.getSum()-1);
				System.out.println(productInfo);
				skuMapper.updateById(productInfo);
				//生成订单号
				String orderId = UUID.randomUUID().toString();
				order.setOrderId(orderId);
				//订单状态
				order.setOrderStatus((byte)1);
				//创建时间
				order.setOrderTime(new Date());
				order.setCreateTime(new Date());
				createOrder=order;
				orderMapper.insert(order);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			//解锁
			lock.unlock();
		}
		return createOrder;
	}

	@Override
	public boolean redisDecrementOroduct(Order order) {
			String key="order"+order.getOrderId();
			String value= JSON.toJSONString(order);
		return redisUtil.set(key,value,1800);
	}
}
